package com.lvyq.myplayer.ui.activity.singer

import android.os.Build
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.lvyq.myplayer.R
import com.lvyq.myplayer.base.BaseActivity
import com.lvyq.myplayer.common.util.MockDataUtil
import com.lvyq.myplayer.parcelable.SingerInfoData
import com.lvyq.myplayer.ui.fragment.singer.SingerAlbumPagerFragment
import com.lvyq.myplayer.ui.fragment.singer.SingerSongPagerFragment
import com.lvyq.myplayer.ui.fragment.singer.SingerVideoPagerFragment
import com.squareup.picasso.Picasso

/**
 * 歌手详情页
 */
class SingerIndexActivity : BaseActivity() {

    var singerData:SingerInfoData?=null
    lateinit var singerView: TextView
    lateinit var singerImgView: ImageView
    lateinit var typeTabView:TabLayout
    lateinit var songTypeView:ViewPager2

    var songTypeLst=MockDataUtil.songTypeList()
    override fun getLayoutId(): Int {
       return R.layout.activity_singer_info
    }


    override fun initData() {
        singerView=  findViewById(R.id.singer)
        singerImgView =findViewById(R.id.singerImg)
        typeTabView =findViewById(R.id.typeTab)
        songTypeView = findViewById(R.id.songTypeView)
        // （兼容所有版本）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            singerData=  intent.getParcelableExtra("singerInfo", SingerInfoData::class.java)
        } else {
            @Suppress("DEPRECATION")
            singerData=intent.getParcelableExtra("singerInfo") as? SingerInfoData
        }

        singerView.text=singerData?.name
        Picasso.with(this).load(singerData?.img).into(singerImgView)

        // 设置 ViewPager2 适配器
        songTypeView.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = songTypeLst.size

            override fun createFragment(position: Int): Fragment {
                if (position==0){
                    return SingerSongPagerFragment(singerData?.id?:-1)
                }
                if (position==1){
                    return SingerAlbumPagerFragment(singerData?.id?:-1)
                }
                if (position==2){
                    return SingerVideoPagerFragment(singerData?.name?:"")
                }
                return SingerSongPagerFragment(singerData?.id?:-1)
            }

        }

        // 关联 TabLayout 和 ViewPager2
        TabLayoutMediator(typeTabView, songTypeView) { tab, position ->
            tab.text = songTypeLst[position].name.toString()
        }.attach()
        // 默认选中第一个页面
        songTypeView.setCurrentItem(0, true)
    }

}